package com.poh.controller;

import com.github.pagehelper.PageInfo;
import com.poh.pojo.Course;
import com.poh.pojo.User;
import com.poh.pojo.query.UserQuery;
import com.poh.service.impl.CourseServiceImpl;
import com.poh.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserTeacherController {
    @Autowired
    CourseServiceImpl courseService;
    @Autowired
    UserServiceImpl userService;

    @PostMapping("/teacherAddCourse")
    public String addCourse(HttpSession session,
                            Course course) {
        User user = (User) session.getAttribute("user");
        course.setCourseCreatedId(user.getUserId());
        courseService.createCourse(course);
        return "redirect:/getCourse";
    }

//    @GetMapping("/")
//    public String index(Model model, UserQuery userQuery){
//        PageInfo<User> userPageInfo = userService.listUserByName(userQuery);
//        model.addAttribute("page",userPageInfo);
//        return "index";
//    }

    @GetMapping("/getStudent/{courseId}")
    public String getStudent(@PathVariable("courseId") int courseId, UserQuery userQuery,
                             Model model, HttpSession session) {
        String searchInput = (String) session.getAttribute("searchInput");
        if (searchInput != null || searchInput != "") {
            userQuery.setName(searchInput);
        }
        List<User> allStudent = userService.getAllStudent();
        List<User> users = userService.getStudentByCourseId(courseId);
        PageInfo<User> userPageInfo = userService.listUserByName(userQuery);
        model.addAttribute("page", userPageInfo);
        model.addAttribute("courseId", courseId);
        model.addAttribute("users", users);
        model.addAttribute("allStudent", allStudent);
        String error = (String) session.getAttribute("error");
        if (error != null) {
            model.addAttribute("error", error);
            session.removeAttribute("error");
        }
        return "user/studentListFromCourse";
    }

//    @PostMapping("/searchStudent")
    @PostMapping("/getStudent/{courseId}")
    public String listUserByName(@PathVariable("courseId") int courseId,
                                 Model model,UserQuery userQuery,
                                 HttpSession session){
        model.addAttribute("courseId", courseId);
        PageInfo<User> userPageInfo = userService.listUserByName(userQuery);
        session.setAttribute("searchInput", userQuery.getName());
        model.addAttribute("page",userPageInfo);
        return "user/studentListFromCourse";
    }


    @PostMapping("/addStudent")
    public String addStudent(HttpSession session, User user) {
        Course course = (Course) session.getAttribute("course");
        userService.addStudentToCourse(user.getUserId(), course.getCourseId());
        return "redirect:/getStudent/" + course.getCourseId();
    }


    @GetMapping("/deleteStudent/{userId}")
    public String deleteStudent(@PathVariable("userId") int userId,
                                Model model,
                                HttpSession session) {
        Course course = (Course) session.getAttribute("course");
        userService.deleteUserById(userId, course.getCourseId());
        model.addAttribute("courseId", course.getCourseId());
        return "redirect:/getStudent/" + course.getCourseId();
    }

//    @PostMapping("/searchStudent")
//    public String searchStudent(@RequestParam("userName") String userName,
//                                HttpSession session,
//                                Model model) {
//        Course course = (Course) session.getAttribute("course");
//        List<User> users = userService.queryStudentLikeStudentName(course.getCourseId(), userName);
//        model.addAttribute("users", users);
//        return "/user/studentListFromCourse";
//    }

    @GetMapping("/toTeacherCourse/{courseId}")
    public String toStudentCourse(@PathVariable("courseId") int courseId,
                                  HttpSession session) {
        Course course = courseService.getCourseByCourseId(courseId);
        session.setAttribute("course", course);
        return "course/teacherCourse";
    }



    @PostMapping("/addMoreStudent")
    public String addMoreStudent(@RequestParam("userId")List<Integer> userId,
                                 HttpSession session,
                                 Model model) {
        Course course = (Course) session.getAttribute("course");
        for (Integer id : userId) {
            try {
                userService.addStudentToCourse(id, course.getCourseId());
            } catch (Exception e) {
                session.setAttribute("error", userService.getUserById(id).getUserName()+"同学已经存在");
                System.out.println("错误");
            }
        }
        return "redirect:/getStudent/" + course.getCourseId();
    }

}

